package com.bh.task;

import com.bh.common.constant.Entity;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.StringUtils;
import com.bh.extend.domain.JcCustomer;
import com.bh.extend.service.IJcCustomerService;
import com.bh.extend.service.IJcDbProductService;
import com.bh.ledger.service.IJcLedgerDetailService;
import com.bh.ledger.vo.JcLedgerDetailGenerateVo;
import com.bh.order.domain.JcOrder;
import com.bh.order.dto.JcOrderQueryDto;
import com.bh.order.service.IJcOrderService;
import com.bh.order.vo.JcOrderVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 定时任务调度,通过定时任务生成数据
 *
 * @author ruo-yi
 */
@Component("orderTask")
public class OrderTask {
    @Resource
    private IJcLedgerDetailService jcLedgerDetailService;
    @Resource
    private IJcOrderService orderService;

    public void batchInsert() {
        /**
         * 查询最后一条数据，并按照时间找前一天的数据，将查询到的不同的数据插入
         */
        System.out.println(StringUtils.format("执行多参方法"));
    }


    /**
     * 每天 凌晨1:00
     * 批量生成上一天账单
     */
    public void batchSaveLedgerDetail() {
        /**
         * 查询最后一条数据，并按照时间找前一天的数据，将查询到的不同的数据插入
         */
        System.out.println(StringUtils.format("执行多参方法"));
        JcOrderQueryDto jcCustomer = new JcOrderQueryDto();
        jcCustomer.setTime(DateUtils.addDays(new Date(), -1));
        // 查询本日存在订单的客户
        List<JcOrderVO> orders = orderService.selectJcOrderVoList(jcCustomer);
        if (orders != null && !orders.isEmpty()) {
            List<Long> customerIds = orders.stream().map(JcOrderVO::getCustomerId).distinct().toList();
            for (Long customerId : customerIds) {
                JcLedgerDetailGenerateVo generateVo = new JcLedgerDetailGenerateVo();
                generateVo.setJcCustomerId(customerId);
                generateVo.setStartDate(new Date());
                generateVo.setEndDate(new Date());
                jcLedgerDetailService.manualGenerate(generateVo);
            }
        }
    }
}
